package com.tiantianyuegou.util;

import android.annotation.SuppressLint;
import android.text.format.DateFormat;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


@SuppressLint("SimpleDateFormat")
public class DateUtil {
	
	public static final long DAY_MILLS = 1000L * 60 * 60 * 24;
	public static final long WEEK_MILLS = 1000L * 60 * 60 * 24 * 7;
	public static final long MONTH_MILLS = 1000L * 60 * 60 * 24 * 30;
	public static final long YEAR_MILLS = 1000L * 60 * 60 * 24 * 30 * 12;
	public static final String EXMPL = "yyyy-MM-dd hh:mm:ss";

//	 /**
//     * 格式化日�??
//     * @param date 日期,Date类型
//     * @param exmpl 格式化表达式
//     * @return 格式化字符串日期
//     */
//    
//	public static String formatDate(Date date , String exmpl) {
//        String strDate = "";
//        if(StringUtil.isNotEmpty(exmpl) && CommonUtil.isNotEmpty(date)) {
//        	try {
//        		SimpleDateFormat formatter = new SimpleDateFormat (exmpl);
//                strDate = formatter.format(date);
//            }
//            catch(Exception e) {
//                e.printStackTrace();
//            }
//        }
//        return strDate;
//    }
    
    /**
	 * 比较两个字符串时�??
	 * @param startDate �??��时间
	 * @param endDate  结束时间
	 * @return 结束时间大于�??��时间为真
	 */
	public static boolean dateCompare(String startDate, String endDate) {
		if (startDate == null || "".equals(startDate) || endDate == null
				|| "".equals(endDate)) {
			return false;
		} else {
			if (startDate.equals(endDate)) 
				return false;

			int fen1 = startDate.indexOf(" ");
			int fen2 = endDate.indexOf(" ");
			int nian1 = startDate.substring(0, fen1).indexOf("-");
			int nian2 = endDate.substring(0, fen2).indexOf("-");
			int yue1 = startDate.substring(nian1 + 1, fen1).indexOf("-")
					+ nian1 + 1;
			int yue2 = endDate.substring(nian2 + 1, fen2).indexOf("-") + nian2
					+ 1;

			int shi1 = startDate.indexOf(":");
			int shi2 = endDate.indexOf(":");
			int min1 = startDate.substring(shi1 + 1).indexOf(":") + shi1 + 1;
			int min2 = endDate.substring(shi2 + 1).indexOf(":") + shi2 + 1;

			if (startDate.substring(0, min1).equals(endDate.substring(0, min2)))
				return false;

			// �??
			if (Integer.parseInt(startDate.substring(0, nian1)) > Integer
					.parseInt(endDate.substring(0, nian2))) 
				return false;
			else if (Integer.parseInt(startDate.substring(0, nian1)) < Integer
					.parseInt(endDate.substring(0, nian2)))
				return true;

			// �??
			if (Integer.parseInt(startDate.substring(nian1 + 1, yue1)) > Integer
					.parseInt(endDate.substring(nian2 + 1, yue2))) 
				return false;
			else if (Integer.parseInt(startDate.substring(nian1 + 1, yue1)) < Integer
					.parseInt(endDate.substring(nian2 + 1, yue2)))
				return true;

			// �??
			if (Integer.parseInt(startDate.substring(yue1 + 1, fen1)) > Integer
					.parseInt(endDate.substring(yue2 + 1, fen2))) 
				return false;
			else if (Integer.parseInt(startDate.substring(yue1 + 1, fen1)) < Integer
					.parseInt(endDate.substring(yue2 + 1, fen2)))
				return true;
			// �??
			if (Integer.parseInt(startDate.substring(fen1 + 1, shi1)) > Integer
					.parseInt(endDate.substring(fen2 + 1, shi2))) 
				return false;
			else if (Integer.parseInt(startDate.substring(fen1 + 1, shi1)) < Integer
					.parseInt(endDate.substring(fen2 + 1, shi2)))
				return true;
			// �??
			if (Integer.parseInt(startDate.substring(shi1 + 1, min1)) > Integer
					.parseInt(endDate.substring(shi2 + 1, min2))) 
				return false;
			else if (Integer.parseInt(startDate.substring(shi1 + 1, min1)) < Integer
					.parseInt(endDate.substring(shi2 + 1, min2)))
				return true;
			// //�??
			if(Integer.parseInt(startDate.substring(min1+1)) >
				Integer.parseInt(endDate.substring(min2+1)))
			return false;
		}
		return true;
	}
	
	/**
	 * 根据字符串时间获取当前时间离1970�??�??号的毫秒�??
	 * @param date
	 * @param exmpl
	 * @return
	 */
	public static long getTime(String date, String exmpl) {
		long time = 0;
		if(StringUtil.isEmpty(exmpl)) {
			exmpl = EXMPL;
		}
		SimpleDateFormat format = new SimpleDateFormat(exmpl);
		try {
			time = format.parse(date).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return time;
	}
	
	/**
	 * 获取当前日期
	 */
	public static String getDate() {
		return DateFormat.format("yyyy-MM-dd", Calendar.getInstance()).toString();
	}
	public static String getDateone(){
		return DateFormat.format("yyyyMMdd", Calendar.getInstance()).toString();
	}

	/**
	 * 获取日期
	 */
	public static String getDate(Calendar c) {
		return DateFormat.format("yyyy-MM-dd", c).toString();
	}
	
	/**
	 * 获取当前时间，默认格式为：HH:mm:ss
	 * 24
	 */
	public static String getTime() {
		return DateFormat.format("kk:mm:ss", Calendar.getInstance()).toString();
	}
	public static String getTimeone() {
		return DateFormat.format("kkmmss", Calendar.getInstance()).toString();
	}
	
	/**
	 * 获取当前时间，默认格式为：yyyy-MM-dd hh:mm:ss
	 * 24
	 */
	public static String getTime(Calendar c) {
		return DateFormat.format("yyyy-MM-dd hh:mm:ss", c).toString();
	}
	
	/**
	 * 获取当前时间，默认格式为：HH:mm:ss
	 * 24
	 */
	public static String getHSTime() {
		return DateFormat.format("kk:mm", Calendar.getInstance()).toString();
	}
	
	/**
	 * 获取当前时间的毫秒数
	 */
	public static long getMillisecond() {
		return new Date().getTime();
	}
	
	/**
	 * 获取当前日期和时间，格式：yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static String getDateTime(){
		return getDate() + " " + getTime();
	}
	public static String getDateTimenone(){
		return getDateone()+getTimeone();
	}
	
	/**
	 * 以指定格式获取当前时�??
	 * @param format
	 * @return
	 */
	public static String getTime(String format){
		return DateFormat.format(format, Calendar.getInstance()).toString();
	}
	
	/**
	 * 获取标准日期显示
	 */
	public static String getFormateDate(int year, int month, int day){
		String systemTime = year + "-" + (month < 10 ? "0" + month : month)
				+ "-" + (day < 10 ? "0" + day : day);
		return systemTime;
	}
	
	public static String getFormateDate2(int year, int month, int day){
		String systemTime = year + "" + (month < 10 ? "0" + month : month)
				+ "" + (day < 10 ? "0" + day : day);
		return systemTime;
	}
	public static String getFormateDate3(int year, int month, int day,int hours,int minutes ){
		String systemTime = year + "-" + (month < 10 ? "0" + month+1 : month+1)
				+ "-" + (day < 10 ? "0" + day : day)+ " " + (hours < 10 ? "0" + hours : hours)+ ":" + (minutes < 10 ? "0" + minutes : minutes);
		return systemTime;
	}

	/**
	 * �??"2012-03-15"时间 转化为int[]
	 * @param s 目标字符�??
	 * @return 转换后的数组
	 */
	public static int[] string2Date(String s) {
		int[] res = new int[3];
		String[] temp1 = s.split("-");
		res[0] = Integer.parseInt(temp1[0]);
		res[1] = Integer.parseInt(temp1[1]);
		res[2] = Integer.parseInt(temp1[2]);
		return res;
	}
	 /** 
     * 根据�? �? 获取对应的月�? 天数 
     * */  
    public static int getDaysByYearMonth(int year, int month) {  
          
        Calendar a = Calendar.getInstance();  
        a.set(Calendar.YEAR, year);  
        a.set(Calendar.MONTH, month - 1);  
        a.set(Calendar.DATE, 1);  
        a.roll(Calendar.DATE, -1);  
        int maxDate = a.get(Calendar.DATE);  
        return maxDate;  
    } 
	
	/**
     * @param date1 �??��比较的时�??格式"yyyy-MM-dd"
     * @param date2 被比较的时间,格式"yyyy-MM-dd"
     * @return   判断两个时间相差了几�??
     */   
    public static int compareDate(String date1, String date2) {    

    	long time1 = getTime(date1, "yyyy-MM-dd");
    	long time2 = getTime(date2, "yyyy-MM-dd");

    	long time = time2 - time1;
    	long day_time = 1000*60*60*24;

    	int day = (int)(time/day_time);
    	return day;
    }
    

    /**
     * @param date �??��比较的时�??格式"yyyy-MM-dd"
     * @return   判断上一次时�??跟现在相差了几天
     */   
    public static int compareDate(String date) {    

    	long time1 = getTime(date, "yyyy-MM-dd");
    	long time2 = getTime(getDate(), "yyyy-MM-dd");

    	long time = time2 - time1;
    	long day_time = 1000*60*60*24;

    	int day = (int)(time/day_time);
    	return day;
    }
    
	

	
	// 返回多久�??
	public static String getShortTime(String dateStr) {
		String shortstring = null;

		Date date = getDateByString(dateStr);
		if(date == null) return shortstring;

		long now = Calendar.getInstance().getTimeInMillis();
		long deltime = (now - date.getTime())/1000;
		long year = 365*24*60*60;
		if(deltime > year) {
			shortstring = (int)(deltime/year) + "年前";
		} else if(deltime > 24*60*60) {
			shortstring = (int)(deltime/(24*60*60)) + "天前";
		} else if(deltime > 60*60) {
			shortstring = (int)(deltime/(60*60)) + "小时�?";
		} else if(deltime > 60) {
			shortstring = (int)(deltime/(60)) + "分前";
		} else if(deltime > 1) {
			shortstring = deltime + "秒前";
		} else {
			shortstring = "1秒前";
		}
		return shortstring;
	}
	
	
	/**
	 * 根据毫秒数得到显示的日期时间字符�??
	 */
	public static String millSecond2String(String formate, long milliseconds){
		return DateFormat.format(formate, milliseconds).toString();
	}
	
	/**
	 * 根据毫秒数得到显示的日期时间字符�??
	 */
	public static String millSecond2String(long milliseconds){
		return DateFormat.format("yyyy-MM-dd kk:mm:ss", milliseconds).toString();
	}

	/**
	 * 根据字符串转换成时间类型
	 */
	public static Date getDateByString(String time) {
		Date date = null;
		if (time == null)
			return date;
		String date_format = "yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat format = new SimpleDateFormat(date_format);
		try {
			date = format.parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	/**
     * 根据字符串时间获取当前时间离1970�??�??号的毫秒�??
     * 
     * @param date
     * @return
     */
    public static long getTime1970(String date) {
        long time = 0;

        SimpleDateFormat format = new SimpleDateFormat(EXMPL);
        try {
            time = format.parse(date).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return time;
    }
}
